library(haven);library(car);library(cjoint)

df <- read.csv("Study2.csv")

##Prepare Data
df$c_pid <- recode(df$c_pid,"2=0")
df$c_educ <- recode(df$c_educ,"2=0")
df$c_china <- recode(df$c_china,"2=0")
df$c_gun <- recode(df$c_gun,"2=0")
df$c_lgbt <- recode(df$c_lgbt,"2=0")
df$c_court <- recode(df$c_court,"2=0")
df$c_choose <- recode(df$c_choose,"2=0")
df$general <- recode(df$trial,"1:10=1;11:15=0")
df$courtType <- recode(df$trial,"1=1;2=2;3=3;4=4;5=5;6=1;7=2;8=3;9=4;10=5;11=1;12=2;13=3;14=4;15=5;")
df$c_rate <- recode(df$c_rate,"4=1;3=2;5=3;2=4;1=5")
df$c_mobil <- recode(df$c_mobil,"4=1;3=2;5=3;2=4;1=5")
scores <- factanal(~c_choose+c_rate+c_mobil,data=df,scores="regression",factors=1)$scores
df <- cbind(df[as.integer(rownames(scores)),],scores)
range01 <- function(x){(x-min(x))/(max(x)-min(x))}
df$c_summary <- range01(df$Factor1)

df$c_copartisan[df$c_pid==1 & df$pid<4] <- 1
df$c_copartisan[df$c_pid==0 & df$pid>4] <- 1
df$c_copartisan[df$c_pid==1 & df$pid>4] <- 0
df$c_copartisan[df$c_pid==0 & df$pid<4] <- 0

df$ideo <- recode(df$ideo,"1:3=0;4=1;5:7=2")

df$Education <- factor(df$c_educ,levels=c(0,1),labels=c("Do Not Help Pay","Gov't Should Help Pay for College"))
df$China <- factor(df$c_china,levels=c(0,1),labels=c("Do Not Place Tariff","Gov't Should Place Tariff on China"))
df$Guns <- factor(df$c_gun,levels=c(0,1),labels=c("No Required Background Check","Gov't Should Require Background Check"))
df$LGBT <- factor(df$c_lgbt,levels=c(0,1),labels=c("No LGBT Discrimination Laws Needed","Gov't Should Protect LGBT from Discrimination"))
df$Court <- factor(df$c_court,levels=c(0,1),labels=c("Do Not Curb Court","Supports Curbing the Court"))
df$PID <- factor(df$c_pid,levels=c(0,1),labels=c("Republican Candidate","Democratic Candidate"))
df$Copartisan <- factor(df$c_copartisan,levels=c(0,1),labels=c("Outpartisan Candidate","Copartisan Candidate"))
df$highLegit <- recode(df$legit,"-100:0=0;.0000000001:100=1")
df$legit3 <- recode(df$legit,"-100:-0.5047286=0;-0.50472861:0.4372574=1;0.43725741:100=2")
df$Legitimacy <- factor(df$legit3,levels=c(0,1,2),labels=c("Low Legitimacy","Moderate Legitimacy","High Legitimacy"))
df$Legitimacy1 <- factor(df$highLegit,levels=c(0,1),labels=c("Low Legitimacy","High Legitimacy"))
df$Party <- recode(df$pid, "1:3=0;5:7=1") #Dropping Independents
df$Party <- factor(df$Party,levels=c(0,1),labels=c("Democrat","Republican"))
df$Ideology <- factor(df$ideo,levels=c(0,1,2),labels=c("Liberal","Moderate","Conservative"))

df1 <- na.omit(df)
df1$judReview <- df1$c_court
df1$judReview[df1$courtType!=1] <- NA
df1$judElect <- df1$c_court
df1$judElect[df1$courtType!=2] <- NA
df1$judTerm <- df1$c_court
df1$judTerm[df1$courtType!=3] <- NA
df1$judSize <- df1$c_court
df1$judSize[df1$courtType!=4] <- NA
df1$judJur <- df1$c_court
df1$judJur[df1$courtType!=5] <- NA
df1$JudicialReview <- factor(df1$judReview,levels=c(0,1),labels=c("Do Not Eliminate Judicial Review","Supports Eliminating Judicial Review"))
df1$JudicialElections <- factor(df1$judElect,levels=c(0,1),labels=c("Do Not Elect Justices","Supports Electing Justices"))
df1$TermLimits <- factor(df1$judTerm,levels=c(0,1),labels=c("Opposes Term Limits","Supports Term Limits"))
df1$ExpandCourt <- factor(df1$judSize,levels=c(0,1),labels=c("Keep Court at 9 Justices","Supports Expanding the Court"))
df1$JurisdictionStripping <- factor(df1$judJur,levels=c(0,1),labels=c("Do Not Strip Jurisdiction","Supports Jurisdiction Stripping"))



##Figure 4
results <- amce(c_choose~Education+China+Guns+LGBT+Court, data=subset(df1,general==0),
                cluster=TRUE, respondent.id="respondent")
pdf("choosePrimary.pdf",width=8,height=4)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13,
     group.order=c("Court","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure 5
results <- amce(c_choose~Education+China+Guns+LGBT+Court+Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent")
pdf("chooseGeneralCopartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13,
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure 6
results <- amce(c_choose~Legitimacy*Education+Legitimacy*China+Legitimacy*Guns+Legitimacy*LGBT+Legitimacy*Court+Legitimacy*Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent",respondent.varying = "Legitimacy")
pdf("chooseLegitimacyInteractionCopartisan.pdf",width=12,height=6)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13, plot.display="interaction",
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()


##Supplemental Information
##Figure B1
results <- amce(c_choose~Education+China+Guns+LGBT+JudicialReview+Copartisan, data=subset(df1,general==1 & courtType==1),
                cluster=TRUE, respondent.id="respondent")
summary(results)
pdf("chooseCourt1Copartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13,
     group.order=c("JudicialReview","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()


##Figure B2
results <- amce(c_choose~Education+China+Guns+LGBT+JudicialElections+Copartisan, data=subset(df1,general==1 & courtType==2),
                cluster=TRUE, respondent.id="respondent")
summary(results)
pdf("chooseCourt2Copartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13,
     group.order=c("JudicialElections","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()


##Figure B3
results <- amce(c_choose~Education+China+Guns+LGBT+TermLimits+Copartisan, data=subset(df1,general==1 & courtType==3),
                cluster=TRUE, respondent.id="respondent")
summary(results)
pdf("chooseCourt3Copartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13,
     group.order=c("TermLimits","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()


##Figure B4
results <- amce(c_choose~Education+China+Guns+LGBT+ExpandCourt+Copartisan, data=subset(df1,general==1 & courtType==4),
                cluster=TRUE, respondent.id="respondent")
summary(results)
pdf("chooseCourt4Copartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13,
     group.order=c("ExpandCourt","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()


##Figure B5
results <- amce(c_choose~Education+China+Guns+LGBT+JurisdictionStripping+Copartisan, data=subset(df1,general==1 & courtType==5),
                cluster=TRUE, respondent.id="respondent")
summary(results)
pdf("chooseCourt5Copartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Pr(Choose Candidate)",
     text.size=13,
     group.order=c("JurisdictionStripping","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B6
results <- amce(c_rate~Education+China+Guns+LGBT+Court+Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent")
pdf("rateGeneralCopartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Candidate Rating",
     text.size=13,
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B7
results <- amce(c_rate~Education+China+Guns+LGBT+Court, data=subset(df1,general==0),
                cluster=TRUE, respondent.id="respondent")
pdf("ratePrimary.pdf",width=8,height=4)
plot(results, xlab="Change in Candidate Rating",
     text.size=13,
     group.order=c("Court","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B8
results <- amce(c_rate~Legitimacy*Education+Legitimacy*China+Legitimacy*Guns+Legitimacy*LGBT+Legitimacy*Court+Legitimacy*Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent",respondent.varying = "Legitimacy")
pdf("rateLegitimacyInteractionCopartisan.pdf",width=12,height=6)
plot(results, xlab="Change in Candidate Rating",
     text.size=13, plot.display="interaction",
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B9
results <- amce(c_mobil~Education+China+Guns+LGBT+Court+Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent")
pdf("mobilGeneralCopartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Mobilization Rating",
     text.size=13,
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B10
results <- amce(c_mobil~Education+China+Guns+LGBT+Court, data=subset(df1,general==0),
                cluster=TRUE, respondent.id="respondent")
pdf("mobilPrimary.pdf",width=8,height=4)
plot(results, xlab="Change in Mobilization Rating",
     text.size=13,
     group.order=c("Court","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B11
results <- amce(c_mobil~Legitimacy*Education+Legitimacy*China+Legitimacy*Guns+Legitimacy*LGBT+Legitimacy*Court+Legitimacy*Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent",respondent.varying = "Legitimacy")
pdf("mobilLegitimacyInteractionCopartisan.pdf",width=12,height=6)
plot(results, xlab="Change in Mobilization Rating",
     text.size=13, plot.display="interaction",
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B12
results <- amce(c_summary~Education+China+Guns+LGBT+Court+Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent")
pdf("summaryGeneralCopartisan.pdf",width=8,height=4)
plot(results, xlab="Change in Candidate Support",
     text.size=13,
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B13
results <- amce(c_summary~Education+China+Guns+LGBT+Court, data=subset(df1,general==0),
                cluster=TRUE, respondent.id="respondent")
pdf("summaryPrimary.pdf",width=8,height=4)
plot(results, xlab="Change in Candidate Support",
     text.size=13,
     group.order=c("Court","Education","China","Guns","LGBT"),colors="black")
dev.off()

##Figure B14
results <- amce(c_summary~Legitimacy*Education+Legitimacy*China+Legitimacy*Guns+Legitimacy*LGBT+Legitimacy*Court+Legitimacy*Copartisan, data=subset(df1,general==1),
                cluster=TRUE, respondent.id="respondent",respondent.varying = "Legitimacy")
pdf("summaryLegitimacyInteractionCopartisan.pdf",width=12,height=6)
plot(results, xlab="Change in Candidate Support",
     text.size=13, plot.display="interaction",
     group.order=c("Court","Copartisan","Education","China","Guns","LGBT"),colors="black")
dev.off()
